package com.zhku.zhny.admin.web.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.zhku.zhny.admin.constant.ConstantFilePath;
import com.zhku.zhny.admin.exception.RemoveException;
import com.zhku.zhny.admin.exception.SearchException;
import com.zhku.zhny.admin.service.AdminService;
import com.zhku.zhny.admin.service.AdminServiceImpl;
import com.zhku.zhny.domain.Introduction;
import com.zhku.zhny.util.BaseServlet;
import com.zhku.zhny.util.FileOperationUtil;

@WebServlet("/info/intro")
public class IntroductionInfoServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;
	
	/**
	 * 添加团队介绍信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addIntro(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Map<String, String[]> map = null;
		try {
			map = FileOperationUtil.upload(request, ConstantFilePath.LOCALINTROFILEPATH);
			Introduction intro = new Introduction();
			BeanUtils.populate(intro, map);
			AdminService adminService = new AdminServiceImpl();
			int i = adminService.addIntroductionInfo(intro);
			if (i > 0) {
				return "redirect:/admin/success.html";
			}else {
				return "redirect:/admin/fail.html";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/admin/fail.html";
		}
	}
	
	/**
	 * 查询出所有介绍信息，前台接口
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void searchIntros(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminService adminService = new AdminServiceImpl();
		try {
			List<Introduction>intros = adminService.findAllIntros();
			Introduction intro = null;
			for (int i = 0; i < intros.size(); i++) {
				intro = intros.get(i);
				// 修改小组图片访问路径
				String newPath = ConstantFilePath.INTRODIRECTORY+intro.getIntroductionImgPath();
				intro.setIntroductionImgPath(newPath);
			}
			String data = JSON.toJSONString(intros);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}

	/**
	 * 查询出所有介绍信息，后台接口
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findIntros(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminService adminService = new AdminServiceImpl();
		try {
			List<Introduction>intros = adminService.findAllIntros();
			String data = JSON.toJSONString(intros);
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(data);
		} catch (SearchException e) {
			e.printStackTrace();
			response.getWriter().print("");
		}
	}

	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void removeIntros(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		AdminService adminService = new AdminServiceImpl();
		boolean result = false;
		try {
			result = adminService.removeIntroByIds(ids);
			if(result) {
				response.getWriter().print(result);
			}else {
				response.getWriter().print(result);
			}
		} catch (RemoveException e) {
			e.printStackTrace();
			response.getWriter().print(result);
		}
	}
	
}
